package com.infoland.afanmall.model.zhongChou.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ToxicBakery.viewpager.transforms.DefaultTransformer;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.banner.HolderCreator;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.zhongChou.entity.ZhongChouBanner;
import com.infoland.afanmall.model.zhongChou.fragment.ListFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * @author 王永吉
 * @date 2018/7/12 8:20
 * 众筹界面
 */
public class ZhongChouActivity extends AppCompatActivity {
    @BindView(R.id.zhong_chou_tab)
    SlidingTabLayout msgTab;
    @BindView(R.id.zhong_chou_page)
    ViewPager msgViewPage;
    @BindView(R.id.info_back)
    ImageView infoBack;
    @BindView(R.id.info_close_all)
    ImageView infoCloseAll;
    @BindView(R.id.info_text)
    TextView infoText;
    @BindView(R.id.info_dian_dian_dian)
    ImageView infoDianDianDian;
    @BindView(R.id.info_split)
    LinearLayout infoSplit;
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.zhong_chou_banner)
    ConvenientBanner<String> zhongChouBanner;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.avi_pt)
    AVLoadingIndicatorView aviPt;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"综合推荐", "即将上线", "即将结束", "正在进行", "人数最多", "金额最多"};
    int activityId = 0;
    int now_postion = 0;

    private int getLayout() {
        return R.layout.zhong_chou_activity;
    }

    //初始方法
    private void initData() {
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            loadZhongChou();
        });
        getBanner();
    }

    //获取Banner
    private void getBanner() {
        JSONObject para = new JSONObject();
        para.put("placeType", CONST.ZhongChou.BANNER_TYPE);


        new OkHttpKit().send(
                response200 -> {
                    //                getOption();
//                LogKit.E("获取Banner ---->   ", "  " + response);
                    List<ZhongChouBanner> zhongChouBanners = JSONArray.parseArray(response200, ZhongChouBanner.class);
                    setBanner(zhongChouBanners);
//                LogKit.E("getBanner....", "" + list.size());
//                BannerEntity bannerEntity = new BannerEntity();
//                bannerEntity.list = list;
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.BANNER_YX, para, this);


    }

    //设置Banner
    private void setBanner(List<ZhongChouBanner> zhongChouBanners) {
        ArrayList<String> images = new ArrayList<>();
        for (ZhongChouBanner zanner : zhongChouBanners) {
            images.add(zanner.getF_url());
        }
        zhongChouBanner.setOnItemClickListener((position) -> {
        });
        zhongChouBanner.setPages(new HolderCreator(), images)
//                .setPageIndicator(new int[]{R.drawable.dot_normal, R.drawable.dot_focus})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setPageTransformer(new DefaultTransformer())
                .startTurning(3000)
                .setCanLoop(true);
    }

    //添加监听
    private void addLinster() {
        msgViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogKit.E("  onPageSelected ", "  " + position);
                select(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        msgTab.setOnTabSelectListener(new OnTabSelectListener() {
//            @Override
//            public void onTabSelect(int position) {
//                LogKit.E("  onTabSelect ", "  " + position);
//            }
//
//            @Override
//            public void onTabReselect(int position) {
//                LogKit.E("  onTabReselect ", "  " + position);
//
//            }
//
//        });
    }

    private void select(int position) {
        startLoading();
        now_postion = position;
        ((ListFragment) (mFragments.get(position))).initZhongChou();
    }

    //设置Page
    private void setPage() {
        msgTab.setViewPager(msgViewPage, mTitles, this, mFragments);
        msgViewPage.setOffscreenPageLimit(2);
        msgViewPage.setCurrentItem(0);
        now_postion = 0;

    }

    //创建Fragment
    private void addFragment() {
        ListFragment listFragment0 = new ListFragment();
        Bundle bundle0 = new Bundle();
        bundle0.putInt("orderBy", CONST.ZhongChou.ZONG_HE);
        bundle0.putInt("activityId", activityId);
        bundle0.putString("name", "综合推荐");
        listFragment0.setArguments(bundle0);
        //
        ListFragment listFragment1 = new ListFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putInt("orderBy", CONST.ZhongChou.JJ_SHANG_XIAN);
        bundle1.putInt("activityId", activityId);
        bundle1.putString("name", "即将上线");
        listFragment1.setArguments(bundle1);
        //
        ListFragment listFragment2 = new ListFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putInt("orderBy", CONST.ZhongChou.JJ_JIE_SHU);
        bundle2.putInt("activityId", activityId);
        bundle2.putString("name", "即将结束");
        listFragment2.setArguments(bundle2);
        //
        ListFragment listFragment3 = new ListFragment();
        Bundle bundle3 = new Bundle();
        bundle3.putInt("orderBy", CONST.ZhongChou.ZZ_JIN_XING);
        bundle3.putInt("activityId", activityId);
        bundle3.putString("name", "正在进行");
        listFragment3.setArguments(bundle3);
        //
        ListFragment listFragment4 = new ListFragment();
        Bundle bundle4 = new Bundle();
        bundle4.putInt("orderBy", CONST.ZhongChou.RS_ZUI_DUO);
        bundle4.putInt("activityId", activityId);
        bundle4.putString("name", "人数最多");
        listFragment4.setArguments(bundle4);
        //
        ListFragment listFragment5 = new ListFragment();
        Bundle bundle5 = new Bundle();
        bundle5.putInt("orderBy", CONST.ZhongChou.JE_ZUI_DUO);
        bundle5.putInt("activityId", activityId);
        bundle5.putString("name", "金额最多");
        listFragment5.setArguments(bundle5);
//
        listFragment0.setLoadOk((isLastPage) -> {
            stopLoading(isLastPage);
        });
        listFragment0.setRefOk((isLastPage) -> {
//            stopLoading(isLastPage);
        });
        listFragment1.setLoadOk((isLastPage) -> {
            stopLoading(isLastPage);
        });
        listFragment1.setRefOk((isLastPage) -> {
//            stopLoading(isLastPage);
        });
        listFragment2.setLoadOk((isLastPage) -> {
            stopLoading(isLastPage);
        });
        listFragment2.setRefOk((isLastPage) -> {
//            stopLoading(isLastPage);
        });
        listFragment3.setLoadOk((isLastPage) -> {
            stopLoading(isLastPage);
        });
        listFragment3.setRefOk((isLastPage) -> {
//            stopLoading(isLastPage);
        });
        listFragment4.setLoadOk((isLastPage) -> {
            stopLoading(isLastPage);
        });
        listFragment4.setRefOk((isLastPage) -> {
//            stopLoading(isLastPage);
        });
        listFragment5.setLoadOk((isLastPage) -> {
            stopLoading(isLastPage);
        });
        listFragment5.setRefOk((isLastPage) -> {
//            stopLoading(isLastPage);
        });

        mFragments.add(listFragment0);
        mFragments.add(listFragment1);
        mFragments.add(listFragment2);
        mFragments.add(listFragment3);
        mFragments.add(listFragment4);
        mFragments.add(listFragment5);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this);//沉浸黑字
        StatusBarKit.setPaddingSmart(this, titleLayout);
        if (getIntent().getExtras() != null) {
            activityId = getIntent().getExtras().getInt("activityId");
        }
        addFragment();
        setPage();
        addLinster();
        initData();
    }

    //加载 数据
    public void loadZhongChou() {
        ((ListFragment) (mFragments.get(now_postion))).loadZhongChou();
    }

    private void startLoading() {
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadMore(false);
        aviPt.setVisibility(View.VISIBLE);
    }

    private void stopLoading(Boolean isLastPage) {
//        new Handler().postDelayed(() -> {
        smartRefreshLayout.finishLoadMore();
        smartRefreshLayout.finishRefresh();
        aviPt.setVisibility(View.GONE);
        smartRefreshLayout.setVisibility(View.VISIBLE);
        if (isLastPage) {
            smartRefreshLayout.setEnableLoadMore(false);
            ToastUtils.showShort("已经最后一页");
        } else {
            smartRefreshLayout.setEnableLoadMore(true);
        }


//        }, 300);
    }

    @OnClick({R.id.info_back, R.id.info_close_all, R.id.info_dian_dian_dian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.info_back:
                finish();
                break;
            case R.id.info_close_all:
                finish();
                break;
            case R.id.info_dian_dian_dian:
                break;
        }
    }

    @Override
    public void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }
}
